<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

header('Cache-control: no-cache');
header('Content-type: text/html; charset=utf-8');  

$EXCMS = 'work';

ob_start(); 
ob_implicit_flush(0); 

// Start time
$start_time = microtime(); 
$start_array = explode(" ",$start_time); 
$start_time = $start_array[1] + $start_array[0];

// include main files
require_once $_SERVER['DOCUMENT_ROOT'].'/engine/functions.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/engine/connect.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/engine/templates.class.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/engine/templates.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/engine/core.class.php';

// INCLUDE PLUGINS
$plugins = $db->query("SELECT p_file FROM ".DB_PEREFIX."_plugins WHERE p_status = 'on'");
while (list($plugin_file) = $db->fetch_row ($plugins)) if (file_exists($plugin_file)) require_once $_SERVER['DOCUMENT_ROOT'].'/'.$plugin_file;

$theme = $SITE['site_theme'];

// Make template class
$template = new template ($_SERVER['DOCUMENT_ROOT']."/themes/$theme/");
if (is_moderator()) $template->user = "moderator";
elseif (is_admin()) $template->user = "admin";
elseif (is_registered()) $template->user = "register";
else $template->user = "user";

// initialisation core
$CORE = new CORE ($db, $template, $SITE, "mainpage.tpl", $GLOBAL_USER['user_group']);


// connect module
if (!empty($_GET['name'])) {
	$mod_name = addslashes(trim($_GET['name']));
	
	$get_module = $db->query ("SELECT * FROM ".DB_PEREFIX."_modules WHERE module_name = '$mod_name'");
	
	if ($db->num_rows ($get_module) == 1) {
		$MODULE = $db->fetch_array ($get_module);
		$include_module_file = $MODULE['module_file'];
		$PAGE_TITLE = $MODULE['module_header'];
		$CORE->module_name = $mod_name;
		
		// include module plugin
		if ($MODULE['module_plugin'] != "") if (file_exists($MODULE['module_plugin'])) require_once $_SERVER['DOCUMENT_ROOT'].'/'.$MODULE['module_plugin'];
		
		
		// select main template
		if (file_exists($_SERVER['DOCUMENT_ROOT']."/themes/$theme/module_{$mod_name}.tpl")) $CORE->global_template_href = "module_{$mod_name}.tpl";
		elseif (file_exists($_SERVER['DOCUMENT_ROOT']."/themes/$theme/modules_template.tpl")) $CORE->global_template_href = "modules_template.tpl";
		
		
		// SESSIONS
		$ugroup = "guest";
		$uid = "0";
		$unical_sesid = $_SESSION['unical_sesid'];
		if (is_registered()) {
			$ugroup = "user";
			$uid = $GLOBAL_USER['user_id'];
		}
		$CORE->session ($ugroup, $uid, $unical_sesid, $PAGE_TITLE);
			
		
		// include module file
		if ($MODULE['module_status'] == "on") {
			if ($MODULE['module_group'] == "all") {
				if (file_exists($include_module_file)) {
					require_once $_SERVER['DOCUMENT_ROOT'].'/'.$include_module_file;
				}
				else site_error ("Ошибка загрузки файла!");
			}
			
			elseif ($MODULE['module_group'] == "register") {
				if (is_registered()) {
					if (file_exists($include_module_file)) {
						require_once $_SERVER['DOCUMENT_ROOT'].'/'.$include_module_file;
					}
					else site_error ("Ошибка загрузки файла!");
				}
				else error_403 ();
			}
			elseif ($MODULE['module_group'] == "moderator") {
				if (is_moderator() || is_admin()) {
					if (file_exists($include_module_file)) {
						require_once $_SERVER['DOCUMENT_ROOT'].'/'.$include_module_file;
					}
					else site_error ("Ошибка загрузки файла!");
				}
				else error_403 ();
			}
		}
		elseif ($MODULE['module_status'] == "off") site_error ("В данный момент модуль неактивен!");
		
		
	}
	else error_404 ();
		
		
		
		
// include start page (start module) if there is no such module
} else {
	if (file_exists($SITE['site_main'])) if (preg_match("#{body}#is", file_get_contents ( $_SERVER['DOCUMENT_ROOT']."/themes/$theme/".$CORE->global_template_href))) include $_SERVER['DOCUMENT_ROOT'].'/'.$SITE['site_main'];
	else site_error ("Ошибка загрузки файла");
}


$output = ob_get_contents();
ob_end_clean();

$CORE->title = $PAGE_TITLE;
$CORE->body = $output;
$CORE->make_page ();


// stop timer, past it to end of page
$end_time = microtime(); 
$end_array = explode(" ",$end_time); 
$end_time = $end_array[1] + $end_array[0]; 
$time = $end_time - $start_time; 
printf("\n\n<!-- Страница сгенерирована за %f секунд(ы) -->\n",$time);
print "<!-- MySQL запрсов: {$db->count_query} -->\n";
printf("<!-- Время выполнения запросов MySQL: %f секунд(ы) -->\n", $db->past_work_time());
printf("<!-- Обработка шаблонов %f секунд(ы) -->\n", $template->past_work_time());
print "<!-- Использовано {$template->count_} шаблонов -->\n";

mysql_close();
exit; 
